//
//  MyInvestDetailController.m
//  DianDangDai
//
//  Created by bhf on 2018/1/23.
//  Copyright © 2018年 bhf. All rights reserved.
//

#import "MyInvestDetailController.h"
#import "MyInvestCell.h"
#import "AttributedIntString.h"
@interface MyInvestDetailController ()
{
    NSString *_htid;
    NSString *_title;
    NSString *_num;
    NSString *_period;
    NSString *_invest_amount;
    NSString *_apr;
    NSString *_receiving_amount;
    NSString *_repayment_type;
    NSString *_loan_periods;
    NSString *_rest_periods;
    NSString *_receive_time;
    NSString *_current_period;
    NSString *_status;
    NSString *_should_received_amount;
    
}
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *titleArray2;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *dataArray1;
@end

@implementation MyInvestDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleArray = @[@"出借标项目：",@"出借编号：",@"出借期限：",@"出借金额：",@"往期年化利率：",@"出借本息合计：",@"还款方式：",@"出借期数：",@"剩余期数："];
    self.titleArray2 = @[@"本期账单到期时间：",@"本期期数：",@"账单状态：",@"本期账单应收金额："];
    self.title = @"出借账单详情";
    // Do any additional setup after loading the view.
}

- (void)requestData {
    NSLog(@"%@",_investSign);
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1024&condition=%ld&investSign=%@&sign=%@",API_LOGIN_URL_STRING,_type,_investSign,USER_SIGN];
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        NSInteger code = [dict[@"code"] integerValue];
        if (code == -1) {
            [self.view makeToast:dict[@"msg"]];
        } else {
            _title = dict[@"title"];
            _num = dict[@"htid"];
            _period = dict[@"period"];
            _invest_amount = [NSString stringWithFormat:@"%@元",dict[@"invest_amount"]];
            NSNumberFormatter *formatter1 = [[NSNumberFormatter alloc]init];
            formatter1.numberStyle =NSNumberFormatterPercentStyle;
            float apr = [dict[@"apr"] floatValue];
            _apr = [formatter1 stringFromNumber:[NSNumber numberWithFloat:apr/100]];
            float rec = [dict[@"receiving_amount"] floatValue];
            _receiving_amount = [NSString stringWithFormat:@"%.2f元",rec];
            _repayment_type = dict[@"repayment_type"];
            _loan_periods = [NSString stringWithFormat:@"%@期",dict[@"loan_periods"]];
            _rest_periods = [NSString stringWithFormat:@"%@期",dict[@"rest_periods"]];
            _receive_time = dict[@"receive_time"];
            _current_period = [NSString stringWithFormat:@"%@期",dict[@"current_period"]];
            NSInteger status = [dict[@"status"] integerValue];
            if (status) {
                _status = @"计息中";
            } else {
                _status = @"已完成";
            }
            float sra = [dict[@"should_received_amount"] floatValue];
            _should_received_amount = [NSString stringWithFormat:@"%.f元",sra];
            _dataArray = @[_title,_num,_period,_invest_amount,_apr,_receiving_amount,_repayment_type,_loan_periods,_rest_periods];
            _dataArray1 = @[_receive_time,_current_period,_status,_should_received_amount];
            NSLog(@"%@,%@",_dataArray,_dataArray1);
        }
        
        
        //[self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.titleArray.count;
    } else {
        return self.titleArray2.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyInvestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail_cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.titleLabel.text = self.titleArray[indexPath.row];
        if (_dataArray.count != 0) {
            cell.lab.text = _dataArray[indexPath.row];
        }
    } else {
        cell.titleLabel.text = self.titleArray2[indexPath.row];
        if (_dataArray1.count != 0) {
            cell.lab.text = _dataArray1[indexPath.row];
        }
        
    }
    // Configure the cell...
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] init];

    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(4, 19, SCREEN_WIDTH-4, 2)];
    image.image = [self drawLineByImageView:image];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    view.backgroundColor = [UIColor whiteColor];
    [view addSubview:image];
    UILabel *title = [[UILabel alloc] init];
    title.backgroundColor = [UIColor whiteColor];
    title.textColor = THE_MASS_TONE_ATTUNE;
    title.textAlignment = NSTextAlignmentCenter;
    UIImageView *image2 = [[UIImageView alloc] init];
    image2.image = [self drawLineByImageView:image];
    if (section == 0) {
        title.text = @"出借详细信息";
        title.frame = CGRectMake((SCREEN_WIDTH-110)/2, 8, 110, 24);

    } else if (section == 1) {
        title.text = @"本期账单详细信息";
        title.frame = CGRectMake((SCREEN_WIDTH-148)/2, 8, 148, 24);
    }
    
//    [view addSubview:image2];
    [view addSubview:title];
   
    
    return view;
    //view.backgroundColor = [UIColor redColor];

}
//虚线
- (UIImage *)drawLineByImageView:(UIImageView *)imageView{
    UIGraphicsBeginImageContext(imageView.frame.size); //开始画线 划线的frame
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.width, 1)];
    //设置线条终点形状
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    // 5是每个虚线的长度 1是高度
    CGFloat lengths[] = {5,3};
    CGContextRef line = UIGraphicsGetCurrentContext();
    // 设置颜色
    CGContextSetStrokeColorWithColor(line, THE_MASS_TONE_ATTUNE.CGColor);
    CGContextSetLineDash(line, 0, lengths, 2); //画虚线
    CGContextMoveToPoint(line, 0.0, 2.0); //开始画线
    CGContextAddLineToPoint(line, SCREEN_WIDTH - 10, 2.0);
    CGContextStrokePath(line);
    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    return UIGraphicsGetImageFromCurrentImageContext();
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
